package com.taorenqi.business.fragment.mine.more;


import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taorenqi.business.R;
import com.taorenqi.business.fragment.base.BaseBackFragment;
import com.taorenqi.business.utils.StringUtils;
import com.taorenqi.business.utils.ToastUtil;
import com.taorenqi.business.widget.statusview.StatusView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.content.Context.CLIPBOARD_SERVICE;

/**
 * 关于我们
 * A simple {@link Fragment} subclass.
 */
public class AboutUsFragment extends BaseBackFragment {


    @Bind(R.id.status_view)
    StatusView statusView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.about_qq)
    TextView aboutQq;
    @Bind(R.id.about_qq_consult)
    TextView aboutQqConsult;
    @Bind(R.id.about_phone_number)
    TextView aboutPhoneNumber;
    @Bind(R.id.about_phone_number_dial)
    TextView aboutPhoneNumberDial;
    @Bind(R.id.about_wx)
    TextView aboutWx;
    @Bind(R.id.about_wx_copy)
    TextView aboutWxCopy;
    @Bind(R.id.about_email)
    TextView aboutEmail;
    @Bind(R.id.about_email_copy)
    TextView aboutEmailCopy;

    private ClipboardManager cm;
    public static AboutUsFragment newInstance() {
        // Required empty public constructor
        AboutUsFragment fragment = new AboutUsFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_abour_us, container, false);
        ButterKnife.bind(this, view);
        toolbar.setTitle("关于我们");
        initToolbarNav(toolbar);
        cm = (ClipboardManager) _mActivity.getSystemService(CLIPBOARD_SERVICE);
        return attachToSwipeBack(view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.about_qq_consult, R.id.about_phone_number_dial, R.id.about_wx_copy, R.id.about_email_copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.about_qq_consult://咨询QQ
                if(isQQClientAvailable(_mActivity)){
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://wpa.b.qq.com/cgi/wpa.php?ln=2&uin=4006331298")));
                }else{
                    ToastUtil.showToast(_mActivity,"请安装QQ");
                }
                break;
            case R.id.about_phone_number_dial://拨打电话
                call("021-61161542");
                break;
            case R.id.about_wx_copy:
                if (!StringUtils.isEmpty(aboutWx.getText().toString())){
                    cm.setText(aboutWx.getText().toString());
                    ToastUtil.showToast(_mActivity,"复制微信公众号成功");
                }
                break;
            case R.id.about_email_copy:
                if (!StringUtils.isEmpty(aboutEmail.getText().toString())){
                    cm.setText(aboutEmail.getText().toString());
                    ToastUtil.showToast(_mActivity,"复制邮箱成功");
                }
                break;
        }
    }

    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 调用拨号界面
     * @param phone 电话号码
     */
    private void call(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
